import { PopoverState } from '@/components/contract/useWidgetPopover'
import isDateSignSettable from './isDateSignSettable'

/** 日期章设置 */
export default function useDateSignSet(popover: PopoverState) {
  return {
    /** 配置更改则触发事件 `date-sign-set` */
    onChange() {
      const { widget, viewer } = popover
      const options = viewer.signOptions

      if (!widget) return

      /** 如果开启日期章全局配置，则应用当前章配置到其他可设置的控件 */
      if (options.dateSignGlobal) {
        viewer.widgets
          .filter(
            item => item.type === widget.type && item.isUse && item !== widget && isDateSignSettable(options, item)
          )
          .forEach(item => (item.sign = { ...widget.sign }))
      }

      viewer.$emit('date-sign-set', widget, options)
    }
  }
}
